<?php
class index
{
    protected $now;
    protected $limitVersion = 1;
    protected $params = []; // request传参
    protected $cacheTime = 60; //客户端配置缓存时间（秒）

    // 原生广告配置
    protected $nativeConfig = [
        'nativeLockTime'=>15,   //原生广告上锁睡眠时间（秒）
        'nativeClickTime'=>10,  //原生广告页面停留后触发点击时间（秒）
        'nativeRate'=>0,        // 原生广告点击广告的概率（0-100）
        'closeClickRate'=>0,  //点击关闭按钮触发广告点击的概率（0-100）
    ];
    public function __construct()
    {
        $this->now = time();

    }
    // 获取广告位配置
    public function getAdConfig(){
        $nativeConfig = ['x5buc50rh2','h64t5fnc5p','o7ianix4uz','c59h1xk4nt','o5txg7r9r8','k7dlxyr5cy'];
        $nativeConfig = ['testu7m3hc4gvm'];
        $bannerConfig = ['x0y2jh0ppl', 'v5bancmgib', 'q8pe5d8y48', 'l6t8x58yza', 'h0zjnbncsf', 'y8jgwgqpx5', 'n7hjuc63ej', 'n09dcy8x9a'];
        $bannerConfig = ['testw6vs28auh3'];
        $rewardConfig = ['p82elz9p6w','m8ciii9ryk','q3mzb2voqx','f35g9ezytf','y6y6qqcw3p','z3q0ilmwj9'];
        $rewardConfig = ['testx9dtjwj8hp'];
        $adConfig = [
            'native'=>$nativeConfig,
            'banner'=>$bannerConfig,
            'reward'=>$rewardConfig
        ];
        return $adConfig;
    }

    // 底部菜单
    public function getNav(){
        return [
            [
                'name' => 'home',
                'text' => '报名',
                'img' => 'navigate-filled',
                'path' => '/pages/Sign',
                'appId' => ''
            ],
            [
                'name' => 'examine',
                'text' => '做题',
                'img' => 'notification-filled',
                'path' => '/pages/Index',
                'appId' => ''
            ],
            [
                'name' => 'task',
                'text' => '任务',
                'img' => 'notification-filled',
                'path' => '/pages/Task',
                'appId' => ''
            ],
            [
                'name' => 'my',
                'text' => '我的',
                'img' => 'person-filled',
                'path' => '/pages/My',
                'appId' => ''
            ],
        ];
    }

    /**
     * 成功返回
     * @param $info
     * @param array $data
     * @param int $code
     * @throws HttpResponseException
     */
    public function success($info, $data = [], $code = 1): void
    {
        header('Content-Type:application/json; charset=utf-8');
        $result = ['code' => $code, 'info' => $info, 'data' => $data];
        echo json_encode($result);
        die();
    }

    /**
     * 返回失败的操作
     * @param mixed $info 消息内容
     * @param mixed $data 返回数据
     * @param mixed $code 返回代码
     */
    public function error($info, $data = [], $code = 0): void
    {
        header('Content-Type:application/json; charset=utf-8');
        $result = [
            'code' => $code, 'info' => $info, 'data' => $data
        ];
        echo json_encode($result);
        die();
    }


    public function exc(){

        $setting = [];
        $params = $_REQUEST;
        if (empty($params['app_version'])) {
            $params = json_decode(file_get_contents("php://input"), true);
        }
        if (empty($params['app_version'])) {
            $this->error('invalid param');
        }
        $this->params = $params;
        $version = $params['app_version'];
        $switch = $version <= $this->limitVersion ? 1 : 0;
        $setting['switch'] = $switch;
        $setting['cacheTime'] = $this->cacheTime;
        // 当小于限定版本的时候开关开启
        if($switch === 1){
            $setting['adList'] = $this->getAdConfig();
            $setting['bootNav'] = $this->getNav();
            $setting['nativeConfig'] = $this->nativeConfig;
        }
        $this->success('success',$setting);
    }
}

$wap = new index();
$wap->exc();
?>
